<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        obj = {
            name: "adsf"
        }

        Object.defineProperty(obj, "age", {
            get() {
                return obj._age;
            },
            set(val) {
                if (typeof val !== "number") {
                    throw new TypeError("年龄必须是一个数字")
                }
                if (val < 0) {
                    val = 0;
                } else if (val > 200) {
                    val = 200;
                }
                obj._age = val;
            }
        })

        obj.age = "Asdfasasdf";
        console.log(obj.age);
    </script>
</body>

</html>